package org.test.client.ui.content.table.bstable;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellRenderer;

import org.test.client.ui.util.UIUtil;



public class BootStrapTableRender extends JLabel implements TableCellRenderer{
	Color c1 = new Color(Integer.parseInt("ffffff",16));
	Color c2 = new Color(Integer.parseInt("e9f7ff",16));
	Color fontColor = Color.black;
	int marginLeft = 10;
	
	private Object currentValue = null;
	Font plainFont = null;
	Font boldFont = null;
	boolean headerFlag = false;
	
	MyLineBorder topLeft = new MyLineBorder(true,false,false,true);
	MyLineBorder topLeftRight = new MyLineBorder(true, true,false,true);
	MyLineBorder topLeftBottom = new MyLineBorder( true,false,true,true);
	MyLineBorder all = new MyLineBorder(true, true,true,true);
	MyLineBorder topRightBottom = new MyLineBorder(true, true,true,false);
	MyLineBorder topBottom = new MyLineBorder(true, false,true,false);
	
	public BootStrapTableRender(){
		plainFont = new Font(Font.DIALOG, Font.PLAIN, 13);
		boldFont =  new Font(Font.DIALOG, Font.BOLD, 12);
	}
	
	/*
	 * 返回渲染表格的组件，使用该方法在渲染之前配置渲染器。
	 * 参数table：请求渲染器渲染的表，可以为空。
	 * 参数value：要渲染的单元格的值，由渲染器来决定如何解释和渲染该值。比如如果value的值为字符串“true”
	 * 
	 * 渲染器可以渲染成字符串，也可以渲染成一个check box。该值可以为空。
	 * 参数isSelected：当前表格是否被选中，渲染器应据此决定是否应该高亮显示。
	 * 参数hasFocus：当前表格是否拥有焦点，渲染器应据此进行特殊渲染，比如画一个虚线框。
	 * 参数row：当前表格的行号。如果渲染的是表头，该值为－1。 ?*参数column：当前表格的列号。 ?
	 */
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		currentValue = value;
		
		if(value instanceof JComponent ){
			return (JComponent)value;
		}
		
		if(row%2==0){
			this.setBackground(c1);
		}else{
			this.setBackground(c2);
		}
		
		if(row == -1){
			this.setBackground(c1);
			this.setFont(new Font(Font.DIALOG, Font.BOLD, 15));
			this.setPreferredSize(new Dimension(0,30));
		}
		
		if(row == -1){
			headerFlag = true;
			//第一列
			if(column ==0){
				this.setBorder(all);
			}
			//last列
			else if( table.getColumnCount()-1==column){
				this.setBorder(topRightBottom);
			}
			//other
			else{
				this.setBorder(topRightBottom);
			}
		}
		else{
			headerFlag = false;
			this.setBorder(null);
		}
		
		this.setOpaque(true);
		this.setHorizontalAlignment(SwingConstants.LEFT);
		this.setText(value.toString());
		return this;
	}

	
	int diameter = 4;
	int marginLR = 4;
	@Override
	protected void paintComponent(Graphics g) {
//		super.paintComponent(g);
		
		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		
		Icon icon = this.getIcon();
		String txt = this.getText();
		Color color = this.getBackground();
		int www = this.getWidth();
		int hhh = this.getHeight();
		g2d.setColor(color);
		g2d.fillRect(0, 0, www, hhh);
		
		int x = marginLeft;
		int y = 0;
		if(icon != null){
			int iconWidth = icon.getIconWidth();
			int iconHeight = icon.getIconHeight();
		}else{
			y = hhh/2 + UIUtil.getTxty(g2d);
		}
		g2d.setColor(fontColor);
		
		//paint colorful label back
		if(currentValue instanceof ColorfulInfo){
			ColorfulInfo tmp = (ColorfulInfo) currentValue;
			txt = tmp.info;
		
			g2d.setColor(tmp.backColor);
			
			int txtWidth = UIUtil.getTxtWidth(g2d, txt);
			int txtHeight = UIUtil.getTxtHeight(g2d);
			
			int tmpy = y - g2d.getFontMetrics().getAscent() - diameter/2;
			int w = txtWidth + diameter  + marginLR*2 ;
			int h = txtHeight+diameter;
			g2d.fillRoundRect(x, tmpy, w,h, diameter, diameter);
			
			x = x+marginLR + diameter;
			g2d.setColor(tmp.fontColor);
			if(!headerFlag){
				g2d.setFont(boldFont);
			}
		}else{
			if(!headerFlag){
				g2d.setFont(plainFont);
			}
		}
		g2d.drawString(txt, x, y);
	}
	
	public void setNeedVerticalLine(boolean flag){
		topLeft.needVerticalLine = flag;
		topLeftRight.needVerticalLine = flag;
		topLeftBottom.needVerticalLine = flag;
		all.needVerticalLine = flag;
		topRightBottom.needVerticalLine = flag;
		topBottom.needVerticalLine = flag;
	}
}

